/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package minesweeper;

/**
 *
 * @author fanjun
 */
class Map {

    int row;
    int col;
    int has[][];//0表示无雷 1表示有雷
    int mark[][];//-1表示此处是雷 其他数字表示此处不是雷，代表周围9格中雷的数量
    int Nmines;

    Map(int m, int n, int nmines) {
        row = m;
        col = n;
        has = new int[row][col];
        mark = new int[row][col];
        Nmines = nmines;
    }

    void generateMine() {
        int location[] = new int[Nmines];
        int i = 0, j, temp;
        boolean flag;
        while (i < Nmines) {
            temp = (int) (Math.random() * row * col);
            flag = true;
            for (j = 0; j < i; j++) {
                if (temp == location[j]) {
                    flag = false;
                    break;
                }
            }
            if (flag) {
                location[i] = temp;
                System.out.println(location[i]);//////////////////////////////////////////output this,just for test
                has[temp / col][temp % col] = 1;
                i++;
            }
        }

    }

    void generateMark() {
        int i, j;
        for (i = 0; i < row; i++) {
            for (j = 0; j < col; j++) {
                if (has[i][j] == 0) {
                    if (rangeRow(i - 1) && rangeCol(j - 1)) {
                        if (has[i - 1][j - 1] == 1) {
                            mark[i][j]++;
                        }
                    }
                    if (rangeRow(i - 1) && rangeCol(j)) {
                        if (has[i - 1][j] == 1) {
                            mark[i][j]++;
                        }
                    }
                    if (rangeRow(i - 1) && rangeCol(j + 1)) {
                        if (has[i - 1][j + 1] == 1) {
                            mark[i][j]++;
                        }
                    }
                    if (rangeRow(i) && rangeCol(j - 1)) {
                        if (has[i][j - 1] == 1) {
                            mark[i][j]++;
                        }
                    }
                    if (rangeRow(i) && rangeCol(j + 1)) {
                        if (has[i][j + 1] == 1) {
                            mark[i][j]++;
                        }
                    }
                    if (rangeRow(i + 1) && rangeCol(j - 1)) {
                        if (has[i + 1][j - 1] == 1) {
                            mark[i][j]++;
                        }
                    }
                    if (rangeRow(i + 1) && rangeCol(j)) {
                        if (has[i + 1][j] == 1) {
                            mark[i][j]++;
                        }
                    }
                    if (rangeRow(i + 1) && rangeCol(j + 1)) {
                        if (has[i + 1][j + 1] == 1) {
                            mark[i][j]++;
                        }
                    }
                } else {
                    mark[i][j] = -1;
                }
            }
        }
    }

    void print(int a[][], int m, int n) {
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                System.out.print(a[i][j]);
                System.out.print(" ");
            }
            System.out.print('\n');
        }
    }

    boolean rangeRow(int a) {
        if (a >= 0 && a <= row - 1) {
            return true;
        } else {
            return false;
        }
    }

    boolean rangeCol(int a) {
        if (a >= 0 && a <= col - 1) {
            return true;
        } else {
            return false;
        }
    }
}


